home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Misc / DXErr / dxerr.cpp next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  11.6 KB  |  319 lines

  1. //----------------------------------------------------------------------------
  2. // File: DXErr.cpp
  3. //
  4. // Desc: The DXErr sample allows users to enter a numberical HRESULT and get back
  5. //       the string match its define.  For example, entering 0x8878000a will
  6. //       return DSERR_ALLOCATED.
  7. //
  8. // Copyright (c) Microsoft Corp. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <windows.h>
  12. #include <math.h>
  13. #include <basetsd.h>
  14. #include <dxerr9.h>
  15. #include <tchar.h>
  16. #include "resource.h"
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. // Function-prototypes
  24. //-----------------------------------------------------------------------------
  25. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  26. VOID OnInitDialog( HWND hDlg );
  27. VOID LookupValue( HWND hDlg );
  28. BOOL g_bUseHex = TRUE;
  29.  
  30.  
  31.  
  32.  
  33. //-----------------------------------------------------------------------------
  34. // Defines, constants, and global variables
  35. //-----------------------------------------------------------------------------
  36.  
  37. // Older winerror.h may not have all of these, so include them here 
  38. // so this app can build on older Platform SDKs
  39. #define FACILITY_WINDOWS_CE              24
  40. #define FACILITY_WINDOWS                 8
  41. #define FACILITY_URT                     19
  42. #define FACILITY_UMI                     22
  43. #define FACILITY_SXS                     23
  44. #define FACILITY_STORAGE                 3
  45. #define FACILITY_STATE_MANAGEMENT        34
  46. #define FACILITY_SSPI                    9
  47. #define FACILITY_SCARD                   16
  48. #define FACILITY_SETUPAPI                15
  49. #define FACILITY_SECURITY                9
  50. #define FACILITY_RPC                     1
  51. #define FACILITY_WIN32                   7
  52. #define FACILITY_CONTROL                 10
  53. #define FACILITY_NULL                    0
  54. #define FACILITY_MSMQ                    14
  55. #define FACILITY_MEDIASERVER             13
  56. #define FACILITY_INTERNET                12
  57. #define FACILITY_ITF                     4
  58. #define FACILITY_HTTP                    25
  59. #define FACILITY_DPLAY                   21
  60. #define FACILITY_DISPATCH                2
  61. #define FACILITY_CONFIGURATION           33
  62. #define FACILITY_COMPLUS                 17
  63. #define FACILITY_CERT                    11
  64. #define FACILITY_BACKGROUNDCOPY          32
  65. #define FACILITY_ACS                     20
  66. #define FACILITY_AAF                     18
  67.  
  68. #define FACILITY_D3DX                    0x877
  69. #define FACILITY_D3D                     0x876
  70.  
  71.  
  72.  
  73.  
  74. //-----------------------------------------------------------------------------
  75. // Name: WinMain()
  76. // Desc: Entry point for the application.  Since we use a simple dialog for 
  77. //       user interaction we don't need to pump messages.
  78. //-----------------------------------------------------------------------------
  79. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  80.                       INT nCmdShow )
  81. {
  82.     // Display the main dialog box.
  83.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  84.  
  85.     return TRUE;
  86. }
  87.  
  88.  
  89.  
  90.  
  91. //-----------------------------------------------------------------------------
  92. // Name: MainDlgProc()
  93. // Desc: Handles dialog messages
  94. //-----------------------------------------------------------------------------
  95. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  96. {
  97.     switch( msg ) 
  98.     {
  99.         case WM_INITDIALOG:
  100.             OnInitDialog( hDlg );
  101.             break;
  102.  
  103.         case WM_COMMAND:
  104.             switch( LOWORD(wParam) )
  105.             {
  106.                 case IDC_LOOKUP:
  107.                     LookupValue( hDlg );
  108.                     break;
  109.  
  110.                 case IDC_HEX:
  111.                     g_bUseHex = TRUE;
  112.                     break;
  113.  
  114.                 case IDC_DECIMAL:
  115.                     g_bUseHex = FALSE;
  116.                     break;
  117.  
  118.                 case IDCANCEL:
  119.                     EndDialog( hDlg, IDCANCEL );
  120.                     break;
  121.  
  122.                 default:
  123.                     return FALSE; // Didn't handle message
  124.             }
  125.             break;
  126.  
  127.         case WM_DESTROY:
  128.             break; 
  129.  
  130.         default:
  131.             return FALSE; // Didn't handle message
  132.     }
  133.  
  134.     return TRUE; // Handled message
  135. }
  136.  
  137.  
  138.  
  139.  
  140. //-----------------------------------------------------------------------------
  141. // Name: OnInitDialog()
  142. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  143. //-----------------------------------------------------------------------------
  144. VOID OnInitDialog( HWND hDlg )
  145. {
  146.     // Load the icon
  147. #ifdef _WIN64
  148.     HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( hDlg, GWLP_HINSTANCE );
  149. #else
  150.     HINSTANCE hInst = (HINSTANCE) GetWindowLong( hDlg, GWL_HINSTANCE );
  151. #endif
  152.     HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  153.  
  154.     // Set the icon for this dialog.
  155.     SendMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  156.     SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  157.  
  158.     CheckDlgButton( hDlg, IDC_HEX, BST_CHECKED );
  159.  
  160.     SendMessage( hDlg, EM_LIMITTEXT, 20, 0 );  
  161. }
  162.  
  163.  
  164.  
  165. //-----------------------------------------------------------------------------
  166. // Name: LookupValue()
  167. // Desc: 
  168. //-----------------------------------------------------------------------------
  169. VOID LookupValue( HWND hDlg )
  170. {
  171.     HRESULT hrErr = 0;
  172.     TCHAR strValue[MAX_PATH];
  173.     const TCHAR* strHRESULT;
  174.     const TCHAR* strDescription;
  175.     TCHAR strHRESULTCopy[MAX_PATH*2];
  176.     int nIndex;
  177.     int nPower = 0;
  178.     int nPowerDec = 1;
  179.     int nDigit = 0;
  180.     GetDlgItemText( hDlg, IDC_VALUE, strValue, MAX_PATH );
  181.  
  182.     nIndex = lstrlen(strValue) - 1;
  183.  
  184.     // skip whitespace
  185.     while( nIndex >= 0 )
  186.     {
  187.         if( strValue[nIndex] != ' ' && 
  188.             strValue[nIndex] != 'L' )
  189.             break;
  190.             
  191.         nIndex--;
  192.     }
  193.  
  194.     bool bFoundDigit = false;
  195.     while( nIndex >= 0 )
  196.     {
  197.         // Convert to uppercase
  198.         if( strValue[nIndex] >= 'a' && strValue[nIndex] <= 'z' )
  199.             strValue[nIndex] += 'A' - 'a';
  200.  
  201.         if( g_bUseHex && strValue[nIndex] >= 'A' && strValue[nIndex] <= 'F' )
  202.             nDigit = strValue[nIndex] - 'A' + 10;
  203.         else if( strValue[nIndex] >= '0' && strValue[nIndex] <= '9' )
  204.             nDigit = strValue[nIndex] - '0';
  205.         else if( strValue[nIndex] == '-' )
  206.         {
  207.             hrErr = -hrErr;
  208.             break;
  209.         }
  210.         else
  211.         {
  212.             // break if we've found a number, but don't break otherwise
  213.             // This will skip any random letters in the front & end of the string
  214.             if( bFoundDigit )
  215.             {
  216.                 break;
  217.             }
  218.             else
  219.             {
  220.                 nIndex--;
  221.                 continue;
  222.             }
  223.         }
  224.  
  225.         bFoundDigit = true;
  226.         if( g_bUseHex )
  227.             hrErr += ( nDigit << (nPower*4) );
  228.         else
  229.             hrErr += ( nDigit * nPowerDec );
  230.  
  231.         nPowerDec *= 10;
  232.         nIndex--;
  233.         nPower++;
  234.     }
  235.  
  236.     if( hrErr == 0 && !bFoundDigit )
  237.     {
  238.         SetDlgItemText( hDlg, IDC_MESSAGE, TEXT("Invalid input") );
  239.     }
  240.     else
  241.     {
  242.         TCHAR strInternals[100];
  243.         TCHAR strFacility[100];
  244.         
  245.         // Use DXErr9.lib to lookup HRESULT.
  246.         strHRESULT = DXGetErrorString9( hrErr );
  247.         wsprintf( strHRESULTCopy, TEXT("HRESULT: 0x%0.8x (%lu)"), 
  248.                     hrErr, hrErr );
  249.         _tcscat( strHRESULTCopy, TEXT("\r\nName: ") );
  250.         _tcscat( strHRESULTCopy, strHRESULT );
  251.  
  252.         strDescription = DXGetErrorDescription9( hrErr );
  253.  
  254.         TCHAR* strTemp;
  255.         while( ( strTemp = _tcschr( strHRESULTCopy, '&') ) != '\0' )
  256.         {
  257.             strTemp[0] = '\r';
  258.             strTemp[1] = '\n';
  259.         }
  260.  
  261.         if( lstrlen(strDescription) > 0 )
  262.         {
  263.             _tcscat( strHRESULTCopy, TEXT("\r\nDescription: ") );
  264.             _tcscat( strHRESULTCopy, strDescription );
  265.         }
  266.  
  267.         wsprintf( strInternals, TEXT("\r\nSeverity code: %s"), 
  268.                     ( HRESULT_SEVERITY(hrErr) == 1 ) ? TEXT("Failed") : TEXT("Success") );
  269.         _tcscat( strHRESULTCopy, strInternals );
  270.  
  271.         int nFacility = HRESULT_FACILITY(hrErr);
  272.         switch( nFacility )
  273.         {
  274.             case FACILITY_WINDOWS_CE: _tcscpy( strFacility, TEXT("FACILITY_WINDOWS_CE") ); break;
  275.             case FACILITY_WINDOWS: _tcscpy( strFacility, TEXT("FACILITY_WINDOWS") ); break;
  276.             case FACILITY_URT: _tcscpy( strFacility, TEXT("FACILITY_URT") ); break;
  277.             case FACILITY_UMI: _tcscpy( strFacility, TEXT("FACILITY_UMI") ); break;
  278.             case FACILITY_SXS: _tcscpy( strFacility, TEXT("FACILITY_SXS") ); break;
  279.             case FACILITY_STORAGE: _tcscpy( strFacility, TEXT("FACILITY_STORAGE") ); break;
  280.             case FACILITY_STATE_MANAGEMENT: _tcscpy( strFacility, TEXT("FACILITY_STATE_MANAGEMENT") ); break;
  281. //            case FACILITY_SSPI: _tcscpy( strFacility, TEXT("FACILITY_SSPI") ); break;
  282.             case FACILITY_SCARD: _tcscpy( strFacility, TEXT("FACILITY_SCARD") ); break;
  283.             case FACILITY_SETUPAPI: _tcscpy( strFacility, TEXT("FACILITY_SETUPAPI") ); break;
  284.             case FACILITY_SECURITY: _tcscpy( strFacility, TEXT("FACILITY_SECURITY or FACILITY_SSPI") ); break;
  285.             case FACILITY_RPC: _tcscpy( strFacility, TEXT("FACILITY_RPC") ); break;
  286.             case FACILITY_WIN32: _tcscpy( strFacility, TEXT("FACILITY_WIN32") ); break;
  287.             case FACILITY_CONTROL: _tcscpy( strFacility, TEXT("FACILITY_CONTROL") ); break;
  288.             case FACILITY_NULL: _tcscpy( strFacility, TEXT("FACILITY_NULL") ); break;
  289.             case FACILITY_MSMQ: _tcscpy( strFacility, TEXT("FACILITY_MSMQ") ); break;
  290.             case FACILITY_MEDIASERVER: _tcscpy( strFacility, TEXT("FACILITY_MEDIASERVER") ); break;
  291.             case FACILITY_INTERNET: _tcscpy( strFacility, TEXT("FACILITY_INTERNET") ); break;
  292.             case FACILITY_ITF: _tcscpy( strFacility, TEXT("FACILITY_ITF") ); break;
  293.             case FACILITY_HTTP: _tcscpy( strFacility, TEXT("FACILITY_HTTP") ); break;
  294.             case FACILITY_DPLAY: _tcscpy( strFacility, TEXT("FACILITY_DPLAY") ); break;
  295.             case FACILITY_DISPATCH: _tcscpy( strFacility, TEXT("FACILITY_DISPATCH") ); break;
  296.             case FACILITY_CONFIGURATION: _tcscpy( strFacility, TEXT("FACILITY_CONFIGURATION") ); break;
  297.             case FACILITY_COMPLUS: _tcscpy( strFacility, TEXT("FACILITY_COMPLUS") ); break;
  298.             case FACILITY_CERT: _tcscpy( strFacility, TEXT("FACILITY_CERT") ); break;
  299.             case FACILITY_BACKGROUNDCOPY: _tcscpy( strFacility, TEXT("FACILITY_BACKGROUNDCOPY") ); break;
  300.             case FACILITY_ACS: _tcscpy( strFacility, TEXT("FACILITY_ACS") ); break;
  301.             case FACILITY_AAF: _tcscpy( strFacility, TEXT("FACILITY_AAF") ); break;
  302.             case FACILITY_D3DX: _tcscpy( strFacility, TEXT("FACILITY_D3DX") ); break;
  303.             case FACILITY_D3D: _tcscpy( strFacility, TEXT("FACILITY_D3D") ); break;
  304.             default: _tcscpy( strFacility, TEXT("Unknown") ); break;
  305.         }
  306.         wsprintf( strInternals, TEXT("\r\nFacility Code: %s (%d)"), 
  307.                     strFacility, HRESULT_FACILITY(hrErr) );
  308.         _tcscat( strHRESULTCopy, strInternals );
  309.  
  310.         wsprintf( strInternals, TEXT("\r\nError Code: 0x%0.4x (%lu)"), 
  311.                     HRESULT_CODE(hrErr), HRESULT_CODE(hrErr) );                   
  312.         _tcscat( strHRESULTCopy, strInternals );
  313.  
  314.         SetDlgItemText( hDlg, IDC_MESSAGE, strHRESULTCopy );
  315.     }
  316.  
  317.     return;
  318. }
  319.